package workbench.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import settings.domain.User;
import util.DateUtil;
import util.JsonUtil;
import util.UUIDUtil;
import workbench.domain.Customer;
import workbench.domain.Tran;
import workbench.domain.TranHistory;
import workbench.exception.AddTranException;
import workbench.exception.CreateTranHistoryException;
import workbench.exception.DeleteException;
import workbench.exception.UpdateException;
import workbench.service.CustomerService;
import workbench.service.TranService;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Controller
public class TranController {
    @Autowired
    private TranService tranService;
    @Autowired
    private CustomerService customerService;
    @RequestMapping(value = "/getUserListForTran")
    public ModelAndView getUserListForTran(){
        List<User> list=tranService.getUserList();
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("userList",list);
        modelAndView.setViewName("forward:/static/workbench/transaction/save.jsp");
        return modelAndView;
    }
    @RequestMapping(value = "/getCustomerName",method = RequestMethod.POST)
    @ResponseBody
    public String getCustomerName(String name) throws JsonProcessingException {
        List<String> list=customerService.getCustomerName(name);
        String json= JsonUtil.writeValueAsString(list);
        return json;
    }
    @RequestMapping(value = "/addTran",method = RequestMethod.POST)
    public ModelAndView addTran(Tran tran, HttpServletRequest req) throws AddTranException {
        System.out.println(tran);
        tran.setId(UUIDUtil.getUUID());
        tran.setCreateBy(((User)req.getSession().getAttribute("userMessage")).getName());
        tran.setCreateTime(DateUtil.dateToString19Char(new Date(System.currentTimeMillis())));
        tranService.addTran(tran);
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("forward:/static/workbench/transaction/index.jsp");
        return modelAndView;
    }
    @RequestMapping(value = "/goTranDetail")
    public ModelAndView goTranDetail(String id){
        Tran tran=tranService.getTranById(id);
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("tranMessage",tran);
        modelAndView.setViewName("forward:/static/workbench/transaction/detail.jsp");
        return modelAndView;
    }
    @RequestMapping(value = "/getTranHistoryListByTranId")
    @ResponseBody
    public String getTranHistoryListByTranId(String tranId) throws JsonProcessingException {
        List<TranHistory> tranHistoryList=tranService.getTranHistoryListByTranId(tranId);
        String json=JsonUtil.writeValueAsString(tranHistoryList);
        return json;
    }
    @RequestMapping(value = "/changeStage",method = RequestMethod.POST)
    @ResponseBody
    public String changeStage(Tran tran,HttpServletRequest req) throws JsonProcessingException, UpdateException, CreateTranHistoryException {
        String userName=((User)req.getSession().getAttribute("userMessage")).getName();
        tran.setEditBy(userName);
        tran.setEditTime(DateUtil.dateToString19Char(new Date(System.currentTimeMillis())));
        tranService.changeStage(tran);
        String json=JsonUtil.writeValueAsString(tran);
        return json;
    }
}
